<?php  /* PROJECTS $Id: index.php,v 1.84.6.7 2006/11/19 22:36:25 gregorerhardt Exp $ */
global $currentTabId, $pstatus;

$currentTabId = $psfilter = isset($_REQUEST['psfilter']) ? $_REQUEST['psfilter'] : P_ACTIVE;

$AppUI->savePlace();

// Load the companies class to retrieved denied companies
require_once( $AppUI->getModuleClass( 'companies' ) );

// Update project status!
if (isset($_GET['update_project_status']) && isset($_GET['project_status']) && isset($_GET['project_id'])) {
	$r = new DBQuery;
	$r->addTable('projects');
	$r->addUpdate('project_status', $_GET['project_status']);
	$r->addWhere('project_id IN ('.implode(',', $_GET['project_id']).')');
	$r->exec();
	$r->clear();
}

// Retrieve any state parameters
if (isset( $_GET['tab'] )) {
	$AppUI->setState( 'ProjIdxTab', $_GET['tab'] );
}
$tab = $AppUI->getState( 'ProjIdxTab' ) !== NULL ? $AppUI->getState( 'ProjIdxTab' ) : 0;
$active = intval( !$AppUI->getState( 'ProjIdxTab' ) );

if (isset( $_POST['company_id'] )) {
	$AppUI->setState( 'ProjIdxCompany', intval( $_POST['company_id'] ) );
}
$company_id = $AppUI->getState( 'ProjIdxCompany' ) !== NULL ?
	$AppUI->getState( 'ProjIdxCompany' ) : $AppUI->user_company;

$company_prefix = 'company_';

if (isset( $_POST['department'] )) {
	$AppUI->setState( 'ProjIdxDepartment', $_POST['department'] );

	//if department is set, ignore the company_id field
	unset($company_id);
}
$department = $AppUI->getState( 'ProjIdxDepartment' ) !== NULL ?
	$AppUI->getState( 'ProjIdxDepartment' ) : $company_prefix.$AppUI->user_company;

// If $department contains the $company_prefix string that it's requesting a company and not a department.
//  So, clear the $department variable, and populate the $company_id variable.
if (!(strpos($department, $company_prefix)===false)) {
	$company_id = substr($department,strlen($company_prefix));
	$AppUI->setState( 'ProjIdxCompany', $company_id );
	unset($department);
}

if (isset( $_GET['orderby'] )) {
	$orderdir = $AppUI->getState( 'ProjIdxOrderDir' ) ?
		($AppUI->getState( 'ProjIdxOrderDir' )== 'asc' ? 'desc' : 'asc' ) : 'desc';
	$AppUI->setState( 'ProjIdxOrderBy', $_GET['orderby'] );
	$AppUI->setState( 'ProjIdxOrderDir', $orderdir);
}
$orderby  = $AppUI->getState( 'ProjIdxOrderBy' ) ? $AppUI->getState( 'ProjIdxOrderBy' ) : 'project_end_date';
$orderdir = $AppUI->getState( 'ProjIdxOrderDir' ) ? $AppUI->getState( 'ProjIdxOrderDir' ) : 'asc';

// prepare the users filter
if (isset( $_POST['show_owner'] ))
	$AppUI->setState( 'ProjIdxowner', intval( $_POST['show_owner'] ) );
$owner = $AppUI->getState( 'ProjIdxowner' ) !== NULL ? $AppUI->getState( 'ProjIdxowner' ) : 0;

$bufferUser = '<select name="show_owner" onchange="document.pickUser.submit()" class="text">';
$bufferUser .= '<option value="0">'.$AppUI->_('All Users');

$q = new DBQuery();
$q->addTable('users', 'u');
$q->addQuery('user_id,user_username,contact_first_name,contact_last_name');
$q->addJoin('contacts', 'c', 'contact_id = user_contact');
$q->addWhere('user_id <> 1');
$q->addOrder('contact_last_name');

if ($rows = $q->loadList()) {
	foreach ($rows as $row) {
		$bufferUser .=  '<option value="'.$row['user_id'].'" '.($owner == $row['user_id'] ? 'selected' : '').'>'.
						$row['contact_first_name'].' '.$row['contact_last_name'].
						'</option>';
	}
}

// Collect the full projects list data via function in projects.class.php
projects_list_data();

$nolist = array(
	P_HOLDING,
	P_COMPLETED,
	P_ARCHIVED,
	P_ALL,
);
if ($AppUI->user_type > 5) { // only Manager & upper level can create Template project
	$nolist[] = P_TEMPLATE;
}

// Setup the title block
$titleBlock = new CTitleBlock( 'Projects' );
$titleBlock->addCell( $AppUI->_('Owner') . ':');
$titleBlock->addCell( $bufferUser, '', '<form action="?m=projects" method="post" name="pickUser">', '</form>');
$titleBlock->addCell( $AppUI->_('Company') . '/' . $AppUI->_('Division') . ':');
$titleBlock->addCell( $buffer, '', '<form action="?m=projects" method="post" name="pickCompany">', '</form>');
$titleBlock->addCell();
if ($canAuthor) {
	$label = in_array($psfilter, $nolist) ? $pstatus[$psf = P_ACTIVE] : $pstatus[$psf = $psfilter];
	$titleBlock->addButton($AppUI->_('new '.$label.' project'),
		'index.php?m=projects&a=addedit&project_status='.$psf);
}
$titleBlock->show();

$counter = array_fill(0, count($pstatus), 0);
if (is_array($projects)) {
	foreach ($projects as $p) ++$counter[$p['project_status']];
}

$showlist = array(
	P_PROPOSED,
	P_PLANNING,
	P_ACTIVE,
	P_COMPLETED,
);
$viewers = array(P_ALL => 'vw_idx_others');
$other_list = '';
$others = 0;

$statistic = '';
foreach ($pstatus as $pkt => $type) {
	$src = 'vw_idx_'.strtolower($type);
	$viewers[$pkt] = file_exists(DP_CURRENT_MODULE_PATH.'/'.$src.'.php') ? $src : 'vw_idx_others';
	$action = 'changeFilter('.$pkt.')';
	if (in_array($pkt, $showlist)) {
		$handler = $tab == 0 && $psfilter != $pkt ? 'class="filter" onclick="'.$action.'"' : 'class="status"';
		$statistic .= '<td nowrap="nowrap" '.$handler.'>'.$type.':</td>'.
					  '<td class="value">'.$counter[$pkt].'</td>';
	} else {
		$style = $psfilter == $pkt ? 'style="font-weight: bold"' : '';
		$other_list .= '<tr class="second" onclick="'.$action.'"><td '.$style.'>'.$counter[$pkt].' '.$type.'</td></tr>';
		$others += $counter[$pkt];
		if ($style != '') $indicator = '* ';
	}
}

$handler = $tab == 0 && $psfilter != P_ALL ?
			'class="filter" onclick="changeFilter('.P_ALL.')"' : 'class="status"';
$info = '<div id="idOtherTypes" style="position: absolute; display: none; visibility: hidden">'.
		($tab == 0 ?
			'<table id="idOtherList" class="list" width="90px">'.
				'<tr><td class="last"><b>Other status</b></td></tr>'.
				$other_list.
			'</table>' : '').
		'</div>'.
		'<table class="tabstatus" border="0" cellpadding="0" cellspacing="2" width="100%">'.
			'<tr>'.
				'<td '.$handler.'>Total:</td>'.
				'<td class="value">'.count($projects).'</td>'.$statistic.
				'<td align="right" width="100%">&nbsp;</td>'.
				'<td class="filter" onclick="showOthers(this)" nowrap="nowrap">'.$indicator.'Others:</td><td>'.$others.'</td>'.
				'<td>&nbsp;</td>'.
			'</tr>'.
		'</table>';

$tabBox = new CTabBox( '?m=projects', $tab );
$tabBox->add($viewers[$psfilter], $pstatus[$psfilter] . ' Projects');

$min_view = true;
$tabBox->add('viewgantt', 'Gantt');
$tabBox->setInfo($info);
$tabBox->show();
?>
<script>
function changeFilter(v)
{
	go('index.php?m=projects&tab=0&psfilter=' + v);
}

function setMode(f) {
	go('index.php?m=projects&psfilter=<?=$psfilter?>&orderby='+f.id);
}

function showOthers(cell)
{
	var d = $('idOtherTypes');
	var visible = d.style.display != 'none';
	if (visible) {
		Control(d).hide();
	} else {
		p = Control(cell).position();
		Control(d).show(p.x, p.y);
		document.onmousedown = function() {
			setTimeout("Control('idOtherTypes').hide()", 150);
			document.onmousedown = null;
		}
	}
}

Table('idOtherList').setNormalHover(1);
</script>
